package com.carleasoft.mps.adu.business.user.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.carleasoft.mps.adu.business.role.bean.po.RoleParamsPo;
import com.carleasoft.mps.adu.business.role.service.AuthorityRoleService;
import com.carleasoft.mps.adu.business.user.bean.entity.AuthorityUserEntity;
import com.carleasoft.mps.adu.business.user.bean.entity.AuthorityUserTypeEntity;
import com.carleasoft.mps.adu.business.user.bean.po.AuthorityUserTypePo;
import com.carleasoft.mps.adu.business.user.dao.AuthorityUserTypeDao;
import com.carleasoft.mps.adu.business.user.service.AuthorityUserService;
import com.carleasoft.mps.adu.business.user.service.AuthorityUserTypeService;
import com.carleasoft.mps.core.exceptions.BusinessException;
import com.carleasoft.mps.core.user.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

import static com.carleasoft.mps.adu.common.constants.BusinessErrorEnum.DUPLICATE_USER_TYPE;
import static com.carleasoft.mps.adu.common.constants.BusinessErrorEnum.DUPLICATE_USER_TYPE_NON;


/**
 * <p>Description: userTypeServiceImpl</p>
 * @since 2020-05-09
 * @author ztm
 */
@Service
public class AuthorityUserTypeServiceImpl extends ServiceImpl<AuthorityUserTypeDao,AuthorityUserTypeEntity> implements AuthorityUserTypeService {
  @Autowired
    AuthorityUserService authorityUserService;
  @Autowired
    AuthorityRoleService authorityRoleService;
    /**
     * 添加用户类型
     * @param authorityUserTypePo
     * @return void
     */
    @Transactional(rollbackFor = {Exception.class})
    @Override
    public void addUserType(AuthorityUserTypePo authorityUserTypePo) {
        AuthorityUserTypeEntity typeEntity = this.getOne(Wrappers.<AuthorityUserTypeEntity>lambdaQuery().eq(AuthorityUserTypeEntity::getUserTypeName, authorityUserTypePo.getUserTypeName())
                .or().eq(AuthorityUserTypeEntity::getUserTypeNumber,authorityUserTypePo.getUserTypeNumber()));
        if(Objects.nonNull(typeEntity)){
            //用户类型名称重复
            throw new BusinessException(DUPLICATE_USER_TYPE);
        }
        //生成userCode
        authorityUserTypePo.setUserTypeCode(IdWorker.getIdStr());
        //vo转entity
        AuthorityUserTypeEntity authorityUserTypeEntity = new AuthorityUserTypeEntity();
        BeanCopier beanUserCopier = BeanCopier.create(AuthorityUserTypePo.class, AuthorityUserTypeEntity.class, false);
        beanUserCopier.copy(authorityUserTypePo,authorityUserTypeEntity,null);
        //新增用户类型
        this.save(authorityUserTypeEntity);
    }

    /**
     * 编辑用户类型
     * @param authorityUserTypePo
     * @return void
     */
    @Transactional(rollbackFor = {Exception.class})
    @Override
    public void updateUserType(AuthorityUserTypePo authorityUserTypePo) {
        AuthorityUserTypeEntity typeEntity = this.getOne(Wrappers.<AuthorityUserTypeEntity>lambdaQuery().eq(AuthorityUserTypeEntity::getUserTypeName, authorityUserTypePo.getUserTypeName())
                .or().eq(AuthorityUserTypeEntity::getUserTypeNumber,authorityUserTypePo.getUserTypeNumber()));
        if(Objects.nonNull(typeEntity) && !authorityUserTypePo.getUserTypeCode().equals(typeEntity.getUserTypeCode())){
            //用户类型名称重复
            throw new BusinessException(DUPLICATE_USER_TYPE);
        }

        //vo转entity
        AuthorityUserTypeEntity authorityUserTypeEntity = new AuthorityUserTypeEntity();
        BeanCopier beanUserCopier = BeanCopier.create(AuthorityUserTypePo.class, AuthorityUserTypeEntity.class, false);
        beanUserCopier.copy(authorityUserTypePo,authorityUserTypeEntity,null);
        //编辑用户类型
        this.update(authorityUserTypeEntity,Wrappers.<AuthorityUserTypeEntity>lambdaUpdate().eq(AuthorityUserTypeEntity::getUserTypeCode,authorityUserTypeEntity.getUserTypeCode()));
    }

    /**
     * 删除用户类型
     * @param userTypeCode
     * @return void
     */
    @Transactional(rollbackFor = {Exception.class})
    @Override
    public void deleteUserType(String userTypeCode) {
       //TODO 用户校验,
        int count=this.authorityUserService.count(Wrappers.<AuthorityUserEntity>lambdaQuery().eq(AuthorityUserEntity::getUserTypeCode,userTypeCode));
        if(count>0) {
            throw new BusinessException(DUPLICATE_USER_TYPE_NON);
        }
        //TODO 权限删除
        RoleParamsPo roleParamsPo = new RoleParamsPo();
        roleParamsPo.setTargetType(Constants.TARGET_USER_TYPE);
        roleParamsPo.setTargetCode(userTypeCode);
        authorityRoleService.deleteRole(roleParamsPo);
        this.remove(Wrappers.<AuthorityUserTypeEntity>lambdaQuery().eq(AuthorityUserTypeEntity::getUserTypeCode,userTypeCode));
    }

    @Override
    public List<AuthorityUserTypeEntity> userTypePage(AuthorityUserTypePo authorityUserTypePo) {
        LambdaQueryWrapper<AuthorityUserTypeEntity> wrapper = Wrappers.<AuthorityUserTypeEntity>lambdaQuery();
        if (StrUtil.isNotBlank(authorityUserTypePo.getKeyword())){
            wrapper.likeRight(AuthorityUserTypeEntity::getUserTypeName,authorityUserTypePo.getKeyword());
        }
        return this.list(wrapper);
    }
    @Override
    public String convertFromCodeToNum(String userTypeCode){
        return this.getOne(Wrappers.<AuthorityUserTypeEntity>lambdaQuery().eq(AuthorityUserTypeEntity::getUserTypeNumber,userTypeCode)).getUserTypeName();
    }
}
